//
//  PersonalInforViewController.m
//  ChengXinBao
//
//  Created by RongZhou on 17/3/21.
//  Copyright © 2017年 RongZhou. All rights reserved.
//

#import "PersonalInforViewController.h"
#import "MineTableViewCell.h"
#import <Masonry.h>
#import <AFNetworking.h>
@interface PersonalInforViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *nameTextfiled;
@property(nonatomic,strong)UITextField *companyTextfiled;
@property(nonatomic,strong)UITextField *resignTextfiled;
@end

@implementation PersonalInforViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.title = @"个人信息";
    [self createUIAboutPersonalDetail];
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.nameTextfiled.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    self.companyTextfiled.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userCompany"];
    self.resignTextfiled.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userIntroduce"];
}
-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
-(void)createUIAboutPersonalDetail{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 70, ScreenWidth, ScreenHeight/7)];
    [self.view addSubview:topView];
    UIImageView *iconImageView = [[UIImageView alloc] init];
    topView.backgroundColor = [UIColor whiteColor];
    iconImageView.image = [UIImage imageNamed:@"defult.png"];
    [topView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView.mas_left).offset(15);
        make.width.mas_equalTo((ScreenHeight/7) - 10);
        make.height.mas_equalTo((ScreenHeight/7) - 10);
        make.centerY.equalTo(topView.mas_centerY);
    }];
    UILabel *topLabel = [[UILabel alloc] init];
    topLabel.textAlignment = NSTextAlignmentLeft;
    topLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    [topView addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(15);
        make.centerY.equalTo(topView.mas_centerY).offset(-20);
        make.right.equalTo(topView.mas_right);
        make.height.mas_equalTo(30);
    }];
    UILabel *botLabel = [[UILabel alloc] init];
    botLabel.textAlignment = NSTextAlignmentLeft;
    botLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userphone"];
    [topView addSubview:botLabel];
    [botLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(15);
        make.centerY.equalTo(topView.mas_centerY).offset(20);
        make.right.equalTo(topView.mas_right);
        make.height.mas_equalTo(30);
    }];

    NSArray *textfiledArra = @[self.resignTextfiled,self.companyTextfiled,self.nameTextfiled];
    NSArray *textArray = @[@"写点个性签名吧...",@"公司名称",@"匿名时使用"];
    NSArray *txtArray = @[@"签名:",@"公司:",@"昵称:"];
    self.resignTextfiled.delegate = self;
    self.nameTextfiled.delegate = self;
    self.companyTextfiled.delegate = self;
    NSMutableArray *viewsArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < textArray.count; i++) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view];
        UILabel *lable = [[UILabel alloc] init];
        lable.text = txtArray[i];
        lable.textAlignment = NSTextAlignmentCenter;
        [view addSubview:lable];
        [lable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left);
            make.top.equalTo(view.mas_top);
            make.bottom.equalTo(view.mas_bottom);
            make.width.mas_equalTo(ScreenWidth/5);
        }];
        UITextField *textfiled = textfiledArra[i];
        textfiled.placeholder = textArray[i];
        [view addSubview:textfiled];
        [textfiled mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lable.mas_right);
            make.right.equalTo(view.mas_right);
            make.top.equalTo(view.mas_top);
            make.bottom.equalTo(view.mas_bottom);
        }];
        [viewsArray addObject:view];
    }
    [viewsArray mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:60 leadSpacing: 2 * (ScreenHeight / 7) + 110 tailSpacing:ScreenHeight - 2 * (ScreenHeight / 7) - 40];
    [viewsArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(ScreenWidth);
    }];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(savePersonalinfor) forControlEvents:UIControlEventTouchUpInside];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo(60);
        make.left.equalTo(self.view.mas_left);
    }];
}
/**
 *  修改保存个人信息
 */
-(void)savePersonalinfor{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    NSDictionary *param = @{@"id":userId,@"name":self.nameTextfiled.text,@"companyName":self.companyTextfiled.text,@"introduce":self.resignTextfiled.text};
    NSString *url = ServiceUrl@"/phoneUserApp/updatePhoneUser";
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dict);
        [[NSUserDefaults standardUserDefaults] setObject:self.nameTextfiled.text forKey:@"userName"];
        [[NSUserDefaults standardUserDefaults] setObject:self.companyTextfiled.text forKey:@"userCompany"];
        [[NSUserDefaults standardUserDefaults] setObject:self.resignTextfiled.text forKey:@"userIntroduce"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark-文本输入框的懒加载
-(UITextField *)nameTextfiled{
    if (_nameTextfiled == nil) {
        _nameTextfiled = [[UITextField alloc] init];
    }
    return _nameTextfiled;
}
-(UITextField *)companyTextfiled{
    if (_companyTextfiled == nil) {
        _companyTextfiled = [[UITextField alloc] init];
    }
    return _companyTextfiled;
}
-(UITextField *)resignTextfiled{
    if (_resignTextfiled == nil) {
        _resignTextfiled = [[UITextField alloc] init];
    }
    return _resignTextfiled;
}
#pragma mark -textfiled的代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
